package com.zfl.practice;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GiveLikeDemo {
    public static void main(String[] args) throws InterruptedException {
        int threadNum = 50;
        CountDownLatch count1 = new CountDownLatch(threadNum);
        CountDownLatch count2 = new CountDownLatch(threadNum);
        CountDownLatch count3 = new CountDownLatch(threadNum);
        CountDownLatch count4 = new CountDownLatch(threadNum);
        GiveLike giveLike = new GiveLike();
        ExecutorService pool = Executors.newCachedThreadPool();
        long start = System.currentTimeMillis();
        for (int i = 0; i < threadNum; i++) {
            pool.execute(()->{
                for (int j = 0; j < 1000000; j++) {
//                    giveLike.giveLike();
//                    giveLike.giveLikeByAtomicLong();
                    giveLike.giveLikeByAccumulator();
//                    giveLike.giveLikeByLongAdder();
                }
                count1.countDown();
            });
        }
        pool.shutdown();
        count1.await();
        long end = System.currentTimeMillis();
        System.out.println("(end-start) = " + (end-start));
        System.out.println("giveLike = " + giveLike.getAccumulatorLike());
        System.out.println("giveLike = " + giveLike.getAdderLike());
    }
}
